package com.pescamillam.consultadeprecios;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ConsultaActivity extends ActionBarActivity {
	
	// Helps us retrieve data from the database
	private AlmacenamientoProductos productsStorage;

	public Button.OnClickListener mScan = new Button.OnClickListener() {
		public void onClick(View v) {
			startBarCodeIntent();
		}
	};

	private void startBarCodeIntent() {
		IntentIntegrator integrator = new IntentIntegrator(this);
		Collection<String> formats =  new ArrayList<String>();
		formats.add("ONE_D_MODE");
		integrator.initiateScan(formats);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (scanResult != null) {
			String contents = intent.getStringExtra("SCAN_RESULT");
			TextView label = (TextView) findViewById(R.id.textView1);
			
			
			try {
				ProductData product = productsStorage.getProductViaCode(contents);
				if (product == null) {
					Random rand = new Random(); 
					product = new ProductData(String.valueOf(rand.nextInt(1000)), contents, rand.nextInt(10000000)/100.0);
					productsStorage.insertarProducto(product);
				} 
				label.setText(product.code + "\r\n" + product.name + ": $" + product.price);
			} catch (RemoteException e) {
				Log.e("ConsultaActivity", "problem getting product", e);
			}
			
			
		} else {
			TextView label = (TextView) findViewById(R.id.textView1);
			label.setText("Operacion Cancelada");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consulta);
		Button botonBuscar = (Button) findViewById(R.id.botonbuscar);
		botonBuscar.setOnClickListener(mScan);
		productsStorage = new AlmacenamientoProductos(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.consulta, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
